package resource;

import cn.dreampie.route.annotation.API;
import cn.dreampie.route.annotation.GET;
import cn.dreampie.route.annotation.POST;
import cn.dreampie.security.Principal;
import cn.dreampie.security.Subject;
import resource.user.model.User;

/**
 * Created by hadong on 15-1-16.
 */
@API("/sessions")
public class SessionResource extends ApiResource {

	@SuppressWarnings("unchecked")
	@GET(value = "/info", des = "当前用户信息")
	public User get() {
		Principal<User> principal = Subject.getPrincipal();
		if (principal != null)
			return principal.getModel();
		else
			return null;
	}

	@POST(value = "/login", des = "用户登录", valid = SigninValidator.class)
	public User login(String username, String password, boolean rememberMe) {
		Subject.login(username, password, rememberMe);
		return (User) Subject.getPrincipal().getModel();
	}

	@GET(value = "/logout", des = "用户退出")
	public RestMsg logout() {
		Subject.logout();
		return new RestMsg();
	}
}
